﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QuanLyCayXanh.Models;

namespace QuanLyCayXanh.Controllers
{
    public class SystemReportController : Controller
    {
        //
        // GET: /Report/
        private QuanLyCayXanhContext db = new QuanLyCayXanhContext();
        public ActionResult StreetAndFamilyTree()
        {
            QuanLyCayXanhContext db = new QuanLyCayXanhContext();
            var familyTrees = db.FamilyTrees.Select(x => new { x.Id, x.Name });
            var streets = db.Streets.Select(x => new { x.Id, x.Name });

            ViewBag.familyTrees = new SelectList(familyTrees, "Id", "Name");
            ViewBag.groupTrees = new SelectList(db.GroupOfTrees, "Id", "Name");

            ViewBag.Street_Id = new SelectList(db.Streets, "Id", "Name");
            ViewBag.Ward_Id = null;
            ViewBag.District_Id = new SelectList(db.Districts, "Id", "Name");

            return View();
        }

        public ActionResult Search(int? districtId, int? wardId, int? streetId, int? familyTreeId, int? groupTreeId)
        {
            QuanLyCayXanhContext db = new QuanLyCayXanhContext();

            int familyTree_Id = familyTreeId ?? -1;
            int street_Id = streetId ?? -1;
            int district_Id = districtId ?? -1;
            int ward_Id = wardId ?? -1;
            int groupTree_Id = groupTreeId ?? -1;

            var query = db.Trees.Where(x => x.Id != 0);
            
           
            if (ward_Id != -1)
            {
                query = query.Where(x => x.Ward_Id == ward_Id);
            }
            else if (district_Id != -1)
            {
                List<int> listWard = db.Wards.Where(w => w.District_Id == district_Id).Select(m => m.Id).ToList();
                query = query.Where(f => listWard.Contains(f.Ward_Id??-1));                
            }

            if (street_Id != -1)
            {
                query = db.Trees.Where(x => x.Street_Id == street_Id);
            }

            if (familyTree_Id != -1)
            {
                query = query.Where(x => x.FamilyTree_Id == familyTree_Id);
            }

            if (groupTree_Id != -1)
            {
                query = query.Where(x => x.GroupOfTree_Id == groupTree_Id);
            }
            string s = query.ToString();

            var list = query.Select(x => new { x.Id, x.PointX, x.PointY });
            return Json(list, JsonRequestBehavior.AllowGet); 
        }

        [HttpPost, ActionName("GetReportTree")]
        public ActionResult GetReportTree(int? id)
        {
            int Tree_Id = id ?? -1;
            if (Tree_Id == -1)
                return null;
            Tree tree = db.Trees.Find(id);
            ViewBag.report = db.Reports.Where(r => r.Tree_Id == id).ToList();
            if (tree == null)
                return null;
            else
                return PartialView(tree);
        }

    }
}
